package cn.z2huo.demo.spring.transactional.dao;

import cn.z2huo.demo.example.user.UserDOExample;
import cn.z2huo.demo.mapper.user.UserMapper;
import cn.z2huo.demo.model.dataobject.user.UserDO;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 *
 * @author z2huo
 */
@Repository
@RequiredArgsConstructor
public class UserDAO {

    private final UserMapper userMapper;

    public List<UserDO> listInvalidUser(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);

        UserDOExample userDOExample = new UserDOExample();
        UserDOExample.Criteria criteria = userDOExample.createCriteria();
        criteria.andValidFlagEqualTo("0");
        userDOExample.setOrderByClause("user_code, id");
        List<UserDO> userDOS = userMapper.selectByExample(userDOExample);
        return userDOS;
    }

    public void updateUserValidFlag(String userCode, Long userId) {
        UserDO userDO = new UserDO().setValidFlag("1");
        UserDOExample userDOExample = new UserDOExample();
        UserDOExample.Criteria criteria = userDOExample.createCriteria();
        criteria.andIdEqualTo(userId);
        criteria.andUserCodeEqualTo(userCode);
        userMapper.updateByExampleSelective(userDO, userDOExample);
    }

}
